package mao.leetcode.others.leetcode206;

public class Solution {

    public ListNode reverseList(ListNode head) {
     //   if (head == null) return head;
     //   if (head.next == null) return head;
        ListNode pre = null;
        while (head != null){
            ListNode temp = head;
            head=head.next;
            temp.next=pre;
            pre=temp;
        }
        return pre;
    }


    //递归
    public ListNode reverseList_1(ListNode head) {
        if (head == null) return head;
        if (head.next == null) return head;
        ListNode reverse = reverseList_1(head.next);
        ListNode temp= reverse;
        while (temp.next != null)
            temp = temp.next;
        temp.next = head;
        head.next=null;
        return reverse;
    }

    public class ListNode {
        int val;
        ListNode next;
        ListNode(int x) { val = x; }
    }

}
